package manager_service.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import manager_mapper.mapper.ManagerMapper;
import manager_mapper.mapper.UserMapper;
import manager_service.exception.ManagerNotExistException;
import manager_service.exception.UserNotExistException;
import manager_service.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import test_common.entity.*;

import java.util.Date;
import java.util.List;


@Service
@Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRED)
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private ManagerMapper managerMapper;

    @Autowired
    private UserMapper userMapper;

    @Transactional(readOnly=true)
    @Override
    public Manager selectManagerByName(Manager manager) throws ManagerNotExistException {
        ManagerExample example=new ManagerExample();
        example.or()
                .andNameEqualTo(manager.getName())
                .andPasswordEqualTo(manager.getPassword());
        List<Manager> managers= null;
        try {
            managers = managerMapper.selectByExample(example);
        } catch (Exception e) {
           throw new ManagerNotExistException("用户不存在，或是密码或登录名错误");
        }
        return managers.isEmpty()?null:managers.get(0);
    }

    @Override
    public List<Manager> showManagers() {
        List<Manager> managers=managerMapper.selectByExample(new ManagerExample());
        return managers;
    }

    @Override
    public PageInfo<User> showUsers(Integer pageNo,Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<User> users=userMapper.selectByExample(new UserExample());
        PageInfo<User> pageUsers = new PageInfo<>(users);
        return pageUsers;
    }

//    @Override
//    public PageInfo<User> searchUsers(String pageNo,String pageSize,String loginName, String nickname, String email, String role, Date createDate, Date createDate2, Date signDate, Date signDate2) {
//        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
//        List<User> users=userMapper.searchUsers(loginName,nickname,email,role,createDate,createDate2,signDate,signDate2);
//        PageInfo<User> searchUsers=new PageInfo<>(users);
//
//        return users.isEmpty()?null:searchUsers;
//    }

    @Override
    public List<User> searchUsers(String loginName, String nickname, String email, String role, Date createDate, Date createDate2, Date signDate, Date signDate2) {

        List<User> users=userMapper.searchUsers(loginName,nickname,email,role,createDate,createDate2,signDate,signDate2);


        return users.isEmpty()?null:users;
    }

    @Override
    public void modifyUserStatus(Integer userId) {

        User user=userMapper.selectByPrimaryKey(userId);
        if (user.getStatus()==1){
            user.setStatus(-1);
        }
        else if (user.getStatus()==-1){
            user.setStatus(1);
        }

        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public void modifyUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User selectUser(String id) throws UserNotExistException {
        try {
            User user=userMapper.selectByPrimaryKey(Integer.parseInt(id));
            return user;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            throw new UserNotExistException("用户不存在");
        }
    }


}
